package cl.clientesws;

import java.util.Date;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

public class AnotacionSOAWS {
	
	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL;
	/*
	*
	*/
	public void coneccionwsPropio(){
	NAMESPACE = "http://ws";
	URL = "http://127.0.0.1:8080/Anotacion/services/AnotacionSOA?wsdl";
	}
	
	
	public String addAnotacion(Date fecha, String descripcion, int idAlumno, int idAnotador, 
			int idTipoAnotacion, int idSubsector) {
		coneccionwsPropio();
		METHOD_NAME = "add";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo addfecha = new PropertyInfo();
		PropertyInfo adddescripcion = new PropertyInfo();
		PropertyInfo addidalumno = new PropertyInfo();
		PropertyInfo addidanotador = new PropertyInfo();
		PropertyInfo addidtipoanotacion = new PropertyInfo();
		PropertyInfo addidsubsector = new PropertyInfo();
		addfecha.setName("fecha");
		addfecha.setValue(fecha);
		addfecha.setType(Date.class);
		adddescripcion.setName("descripcion");
		adddescripcion.setValue(descripcion);
		adddescripcion.setType(String.class);
		addidalumno.setName("idAlumno");
		addidalumno.setValue(idAlumno);
		addidalumno.setType(int.class);
		addidanotador.setName("idAnotador");
		addidanotador.setValue(idAnotador);
		addidanotador.setType(int.class);
		addidtipoanotacion.setName("idTipoAnotacion");
		addidtipoanotacion.setValue(idTipoAnotacion);
		addidtipoanotacion.setType(int.class);
		addidsubsector.setName("idSubsector");
		addidsubsector.setValue(idSubsector);
		addidsubsector.setType(int.class);
		request.addProperty(addfecha);
		request.addProperty(adddescripcion);
		request.addProperty(addidalumno);
		request.addProperty(addidanotador);
		request.addProperty(addidtipoanotacion);
		request.addProperty(addidsubsector);
		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes = true;
		soapEnvelope.setOutputSoapObject(request);
		AndroidHttpTransport aht = new AndroidHttpTransport(URL);
		String datosJsonString;
		try {
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive) soapEnvelope
					.getResponse();
			datosJsonString = resultString.toString();
			return datosJsonString;
		} catch (Exception e) {
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";
		}
		return datosJsonString;
	}
	
	public String findAll(){

		coneccionwsPropio();

		METHOD_NAME = "findAll";

		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo findall = new PropertyInfo();

		//findall.setName("buscar");
		//findall.setValue(buscar);
		//findall.setType(String.class);

		request.addProperty(findall);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";
			return datosJsonString;}
		


	}
	
	
	
}
